package org.dromara.tms.service.impl;

import org.dromara.common.core.enums.OrderType;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.tms.domain.bo.OrderBo;
import org.dromara.tms.domain.vo.OrderVo;
import org.dromara.tms.domain.Order;
import org.dromara.tms.mapper.OrderMapper;
import org.dromara.tms.service.IOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单Service业务层处理
 *
 * @author wulongbo
 * @date 2024-10-26
 */
@RequiredArgsConstructor
@Service
public class OrderServiceImpl implements IOrderService {

    private final OrderMapper baseMapper;

    /**
     * 查询订单
     *
     * @param id 主键
     * @return 订单
     */
    @Override
    public OrderVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询订单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单分页列表
     */
    @Override
    public TableDataInfo<OrderVo> queryPageList(OrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Order> lqw = buildQueryWrapper(bo);
        Page<OrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单列表
     *
     * @param bo 查询条件
     * @return 订单列表
     */
    @Override
    public List<OrderVo> queryList(OrderBo bo) {
        LambdaQueryWrapper<Order> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Order> buildQueryWrapper(OrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Order> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, Order::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), Order::getOrderNo, bo.getOrderNo());
        lqw.eq(StringUtils.isNotBlank(bo.getInboundNo()), Order::getInboundNo, bo.getInboundNo());
        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsNo()), Order::getLogisticsNo, bo.getLogisticsNo());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreType()), Order::getStoreType, bo.getStoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getCountryType()), Order::getCountryType, bo.getCountryType());
        lqw.eq(StringUtils.isNotBlank(bo.getTransportType()), Order::getTransportType, bo.getTransportType());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryMethod()), Order::getDeliveryMethod, bo.getDeliveryMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getOmsGoodsType()), Order::getOmsGoodsType, bo.getOmsGoodsType());
        lqw.eq(StringUtils.isNotBlank(bo.getGoodsType()), Order::getGoodsType, bo.getGoodsType());
        lqw.eq(StringUtils.isNotBlank(bo.getCustomsDeclaration()), Order::getCustomsDeclaration, bo.getCustomsDeclaration());
        lqw.eq(StringUtils.isNotBlank(bo.getDealMethod()), Order::getDealMethod, bo.getDealMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderType()), Order::getOrderType, bo.getOrderType());
        lqw.eq(bo.getPreVolume() != null, Order::getPreVolume, bo.getPreVolume());
        lqw.eq(StringUtils.isNotBlank(bo.getRemake()), Order::getRemake, bo.getRemake());
        lqw.eq(bo.getWarehouseId() != null, Order::getWarehouseId, bo.getWarehouseId());
        lqw.eq(bo.getAddressId() != null, Order::getAddressId, bo.getAddressId());
        lqw.eq(StringUtils.isNotBlank(bo.getBoxLabel()), Order::getBoxLabel, bo.getBoxLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getBoxLabelUrl()), Order::getBoxLabelUrl, bo.getBoxLabelUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getSignatureFormUrl()), Order::getSignatureFormUrl, bo.getSignatureFormUrl());
        lqw.eq(bo.getUnBoxingFlag() != null, Order::getUnBoxingFlag, bo.getUnBoxingFlag());
        lqw.eq(bo.getMeasureTime() != null, Order::getMeasureTime, bo.getMeasureTime());
        lqw.eq(bo.getPackingTime() != null, Order::getPackingTime, bo.getPackingTime());
        lqw.eq(bo.getShippedTime() != null, Order::getShippedTime, bo.getShippedTime());
        lqw.eq(bo.getArrivedTime() != null, Order::getArrivedTime, bo.getArrivedTime());
        lqw.eq(bo.getDeliveryTime() != null, Order::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(bo.getSignedTime() != null, Order::getSignedTime, bo.getSignedTime());
        lqw.eq(bo.getPredictArrivedTime() != null, Order::getPredictArrivedTime, bo.getPredictArrivedTime());
        lqw.eq(bo.getInTransitTime() != null, Order::getInTransitTime, bo.getInTransitTime());
        return lqw;
    }

    /**
     * 新增订单
     *
     * @param bo 订单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OrderBo bo) {
        // 设置用户 + 部门id
        bo.setUserId(LoginHelper.getUserId());
        bo.setDeptId(LoginHelper.getDeptId());

        // 默认订单状态为草稿
        bo.setOrderType(OrderType.DRAFT.getType());


        Order add = MapstructUtils.convert(bo, Order.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单
     *
     * @param bo 订单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OrderBo bo) {
        Order update = MapstructUtils.convert(bo, Order.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Order entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
